import { defineStore } from 'pinia'
import { MenuListType } from '@/types/menu'
import { menuService } from '@/api/menuApi'
import { ElMessage } from 'element-plus'

interface MenuState {
  menuList: MenuListType[]
  menuWidth: string
  menuTableData: any[] // 菜单管理表格数据
  loading: boolean // 加载状态标记
}

export const useMenuStore = defineStore({
  id: 'menuStore',
  state: (): MenuState => ({
    menuList: [],
    menuWidth: '',
    menuTableData: [],
    loading: false
  }),
  getters: {
    getMenuList(): MenuListType[] {
      return this.menuList
    },
    getMenuTableData(): any[] {
      return this.menuTableData
    },
    getLoading(): boolean {
      return this.loading
    }
  },
  actions: {
    setMenuList(list: []) {
      this.menuList = list
    },
    setMenuWidth(width: string) {
      this.menuWidth = width
    },
    
    // 获取菜单管理列表数据
    async fetchMenuListForManagement() {
      // 设置loading状态
      this.loading = true
      
      try {
        console.log('Store: 开始获取菜单管理列表数据')
        
        // 使用API请求获取菜单数据
        const menuList = await menuService.getMenuListForManagement()
        console.log('Store: 获取到的菜单数据:', menuList)
        
        // 确保数据是数组，防止异常数据结构
        if (Array.isArray(menuList)) {
          this.menuTableData = menuList
          console.log('Store: 菜单数据已保存到store, 数量:', menuList.length)
        } else {
          console.error('Store: 获取到的菜单数据不是数组:', menuList)
          this.menuTableData = []
        }
        
        return menuList
      } catch (error) {
        console.error('Store: 获取菜单列表失败:', error)
        this.menuTableData = []
        throw error
      } finally {
        // 无论成功还是失败，都需要关闭loading状态
        this.loading = false
      }
    },
    
    // 添加菜单
    async addMenu(menuData: any) {
      this.loading = true
      try {
        const result = await menuService.addMenu(menuData)
        ElMessage.success('添加菜单成功')
        await this.fetchMenuListForManagement()
        this.loading = false
        return result
      } catch (error: any) {
        this.loading = false
        ElMessage.error(error.message || '添加菜单失败')
        throw error
      }
    },
    
    // 更新菜单
    async updateMenu(menuData: any) {
      this.loading = true
      try {
        const result = await menuService.updateMenu(menuData)
        ElMessage.success('更新菜单成功')
        await this.fetchMenuListForManagement()
        this.loading = false
        return result
      } catch (error: any) {
        this.loading = false
        ElMessage.error(error.message || '更新菜单失败')
        throw error
      }
    },
    
    // 删除菜单
    async deleteMenu(ids: number[]) {
      this.loading = true
      try {
        const result = await menuService.deleteMenu(ids)
        ElMessage.success('删除菜单成功')
        await this.fetchMenuListForManagement()
        this.loading = false
        return result
      } catch (error: any) {
        this.loading = false
        ElMessage.error(error.message || '删除菜单失败')
        throw error
      }
    }
  }
})
